var reqImg1 = "";
var reqImg2 = "";
apiready = function() {
	$api.getUserInf(function(user) {
		$("#realName").val(user.ownerName);
		$("#mobile").val(user.mobile);
	});
	$("#explain").val();

	$("#btn_sub").on('click', function() {
		var repairContent = $("#explain").val();
		var repairContent_length = $("#explain").val().length;
		//alert('小于5个字'+repairContent_length);
		//	alert('小于5个字'+repairContent_length);
		if (repairContent_length < 5) {
			alert('不能小于5个字');
			return;
		}


		var repairImg1 = 'oo';
		var repairImg2 = 'ooo';
		$api.getUserInf(function(repair) {
			var requestData = {
				account: repair.account,
				owerName: repair.ownerName,
				mobile: repair.ownerMobile,
				compounds: repair.compounds,
				address: repair.compounds + repair.zycode + repair.roomNumber,
				repairContent: repairContent,
				repairImg1: reqImg1[0],
				repairImg2: reqImg2[0]
			};
			$api.do_post('/api/ropertyRepair/create', {
				body: JSON.stringify(requestData)
			}, function(datas) {
				if (datas.code === '0') {

					alert("报修成功");
					api.closeWin();
				} else {
					$api.log(datas);
					alert('报修失败');
				}
			});
		});
	});
};

$('#explain').on('focus', function() {
	var text = $('#explain').val();
	if (text === '请输入问题的描述，以便于我们准确定位，尽快处理') {
		$('#explain').val('');
	}
})

/*$('#explain').on('blur',function(){
	var text = $('#explain').val();
	if(!text){
		$('#explain').val('请输入问题的描述，以便于我们准确定位，尽快处理');
	}
})*/

function selectPic(cb) {
	$api.getServerUrl(function(serverUrl) {
		api.getPicture({
			sourceType: 'library',
			encodingType: 'jpg',
			mediaValue: 'pic',
			destinationType: 'url',
			allowEdit: true,
			quality: 50,
			targetWidth: 100,
			targetHeight: 100,
			saveToPhotoAlbum: false
		}, function(ret, err) {
			if (ret) {
				cb(ret.data);
			}
		});
	});
}

function uploadPicApi(imgSrc, progressElm, cb) {
	$api.getServerUrl(function(serverUrl) {
		api.ajax({
			url: serverUrl + '/upload/file?fileId=upimg',
			method: 'post',
			report: true,
			data: {
				files: {
					upimg: imgSrc
				}
			}
		}, function(ret, err) {
			if (ret) {
				if (ret.status == 1) {
					cb(ret);
				} else {
					var progress = ret.progress;
					$('#' + progressElm).css('width', progress);
				}
			} else {
				console.error(err);
			}
		});
	});
}

function uploadPic(id, elm) {
	switch (id) {
		case 1:
			{
				selectPic(function(imgSrc) {
					$("#img1").attr('src', imgSrc);
					uploadPicApi(imgSrc, 'imgPro1', function(ret) {
						$api.log(ret.body);
						reqImg1 = ret.body.urls;
					})

				})
				break;

			}
		case 2:
			{
				selectPic(function(imgSrc) {
					$("#img2").attr('src', imgSrc);
					uploadPicApi(imgSrc, 'imgPro2', function(ret) {
						reqImg2 = ret.body.urls;
					})

				})
				break;
			}
	}
}
/*

	progress: 100,          //上传进度，0.00-100.00
	   status: '',             //上传状态，详见上传状态常量
	   body: ''     

	0            //上传中
	1            //上传完成
	2            //上传失败
		
*/
//$("#img2").attr('src', serverUrl + '/files/' + ret.urls[0]);